package com.hutou.chuangke.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.dialog.NetDailog;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.RequestThreadPool;
import com.hutou.chuangke.net.http.HttpUrl;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.uitl.CommonUtils;
import com.hutou.chuangke.uitl.PreferencesUtility;
import com.hutou.chuangke.uitl.SharedPreferencesUtil;

/**
 * Created by Administrator on 2017/5/8.
 */

public class ModifypasswordActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private EditText old_password_et,new_password_et;
    Toolbar toolbar;
    private TextView commit_tv;
    private String oldpassword,newpassword;
    Requestmopassword requestmopassword;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private NetDailog netDilaog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        initView();
    }

    private void initView() {
        mContext = this;
        netDilaog = new NetDailog(mContext);
        sharedPreferencesUtil = new SharedPreferencesUtil();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setPadding(0, CommonUtils.getStatusHeight(mContext), 0, 0);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.actionbar_back);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        showQuickControl(false);
        commit_tv = (TextView) findViewById(R.id.commit_tv);
        old_password_et = (EditText) findViewById(R.id.old_password_et);
        new_password_et = (EditText) findViewById(R.id.new_password_et);
        commit_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view == commit_tv){
            oldpassword = old_password_et.getText().toString();
            newpassword = new_password_et.getText().toString();
            if(oldpassword.equals("")){
                Toast.makeText(mContext,"请填写旧密码",Toast.LENGTH_SHORT).show();
                return;
            }
            if(newpassword.equals("")){
                Toast.makeText(mContext,"请填写新密码",Toast.LENGTH_SHORT).show();
                return;
            }
            netDilaog.show();
            requestmopassword = new Requestmopassword();
            requestmopassword.execute();
        }
    }

    class Requestmopassword extends AsyncTask<Void , Void ,Boolean> {

        @Override
        protected Boolean doInBackground(Void... voids) {
            try{
                JsonObject jsonObject = HttpUtil.getResposeJsonObject(HttpUrl.UPDATEBYUSERID,
                        User.modiftpassword(MainApplication.id,MainApplication.sid,oldpassword,newpassword));
                String flag = jsonObject.get("flag").getAsString();
                if(flag.equals("1")){
                    return true;
                }else{
                    return  false;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            netDilaog.dismiss();
            if(aBoolean){
                sharedPreferencesUtil.saveData(mContext,sharedPreferencesUtil.PASS_WORD,newpassword);
                Toast.makeText(mContext,"提交成功",Toast.LENGTH_SHORT).show();
                finish();
            }else{
                Toast.makeText(mContext,"提交失败",Toast.LENGTH_SHORT).show();
            }
        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(requestmopassword != null){
            requestmopassword.cancleTask();
        }
    }
}
